package com.sto.stoapp.dataprocess;

import android.annotation.SuppressLint;
import android.content.Context;

import com.sto.stoapp.scan.ScanRuleManager;
import com.sto.stoapp.status.EnumScanCode;
import com.sto.stoapp.status.EnumTableNames;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ProcessScanCode {

    private EnumScanCode STATUS;

    private static ProcessScanCode processScanCode;

    Context context;

    ProcessScanCode(Context context) {
        this.context = context;
    }

    public static ProcessScanCode getInstance(Context context) {
        if (processScanCode == null) {
            processScanCode = new ProcessScanCode(context);
        }

        return processScanCode;
    }

    /**
     * 扫描条码解析
     *
     * @param result
     * @return
     */
    @SuppressLint("NewApi")
    public EnumScanCode ScanCodeResolve(String result,
                                        EnumTableNames TABLE_STATUS) {
        // 条码为空
        if (result.isEmpty()) {
            return EnumScanCode.CODE_EMPTY;
        } else {
            // 判断装车发件,发车扫描,到车扫描 单号前3位是否等于sto
            // 是否判断包号
            if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_LOADCAR_SENDPIECE
                    || TABLE_STATUS == EnumTableNames.STATUS_TABLE_SEND_CAR
                    || TABLE_STATUS == EnumTableNames.STATUS_TABLE_RECEIVE_CAR
                    || TABLE_STATUS == EnumTableNames.STATUS_TABLE_AVIATION_LOADCAR_SENDPIECE
                    || TABLE_STATUS == EnumTableNames.STATUS_TABLE_AVIATION_SEND_CAR
                    || TABLE_STATUS == EnumTableNames.STATUS_TABLE_AVIATION_RECEIVE_CAR
                    || TABLE_STATUS == EnumTableNames.STATUS_TABLE_CENTER_SEND_CAR
                    || TABLE_STATUS == EnumTableNames.STATUS_TABLE_CENTER_RECEIVE_CAR) {
                if (ScanRuleManager.getInstance(context).isCarSealCode(result)) {
                    return EnumScanCode.CODE_SEAL_OK;
                } else {
                    return EnumScanCode.CAR_SEAL_ILLEGAL;
                }

            }

            EnumScanCode tmpType = checkIs330(result);

            if (tmpType == EnumScanCode.CODE_OTHER_OK) {
                // 可能是其他单号

                if (ScanRuleManager.getInstance(context).isWaybillCode(result)) {
                    return EnumScanCode.CODE_WAY_BILL;
                } else if (ScanRuleManager.getInstance(context).isBagCode(
                        result)) {
                    if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_BAGGINGADNSENDPIECE || TABLE_STATUS == EnumTableNames.STATUS_TABLE_AVIATION_SCAN_PACKET_SEND) {
                        return EnumScanCode.BAG_IN_BAG;
                    }
                    // TODO 应该返回包号类型 现在包号 插入一票的时候去判断 统一返回运单号类型
                    return EnumScanCode.CODE_WAY_BILL;
                } else {
                    return EnumScanCode.CODE_ILLEGAL;
                }
            } else {

                return tmpType;
            }

        }
    }

    private boolean is9Num(String data) {
        Pattern p = Pattern.compile("^\\d{9}$|^\\d{10}$");
        Matcher m = p.matcher(data);
        return m.matches();
    }

    int key330[] = new int[]{7, 9, 5, 3, 2, 4, 6, 8, 9};

    int Special330Code[][] = new int[][]{{0, 5}, {1, 0}, {10, 1}};

    final int key330Consult = 11;

    private EnumScanCode checkIs330(String data) {

        if (ScanRuleManager.getInstance(context).isSpecificCode(data)) {

            STATUS = checkCode(data);
            return STATUS;

        }
//        else if (data.startsWith("66")) {
//            // 不是330
//            if (data.startsWith("668")) {
//                STATUS = EnumScanCode.CODE_OTHER_OK;
//                return STATUS;
//            } else {
//                STATUS = checkCode(data);
//                return STATUS;
//            }
//        }
//        else if (data.startsWith("88")) {
//            // 不是330
//            if (data.startsWith("888")) {
//                STATUS = EnumScanCode.CODE_OTHER_OK;
//                return STATUS;
//            } else {
//                STATUS = checkCode(data);
//                return STATUS;
//            }
//        }
        else {
            STATUS = EnumScanCode.CODE_OTHER_OK;
            return STATUS;
        }

    }

    int get330SpecialChecksum(int key) {
        int checkSum = 0xff;
        for (int i[] : Special330Code) {
            if (i[0] == key) {
                checkSum = i[1];
                break;
            }
        }
        return checkSum;
    }

    private EnumScanCode checkCode(String data) {
        int result = 0xff;
        int dataChecksum = 0xfd;

        result = 0;
        String endCode = data.substring(3, 13 - 1);
        dataChecksum = Integer.parseInt(String.valueOf(data
                .charAt(13 - 1)));

        for (int i = 0; i < key330.length; i++) {
            result += Integer.parseInt(String.valueOf(endCode.charAt(i)))
                    * key330[i];
        }

        int remainder = result % key330Consult; // 求余数
        int checkSum = get330SpecialChecksum(remainder);
        // 特殊校验
        if (checkSum != 0xff) {
            if (checkSum == dataChecksum) {
                // 检验正确
                STATUS = EnumScanCode.CODE_WAY_BILL;
            } else {
                // 校验失败(单号错误)
                STATUS = EnumScanCode.CODE_ILLEGAL;
            }
        } else {
            if (dataChecksum == (key330Consult - remainder)) {
                // 检验正确
                STATUS = EnumScanCode.CODE_WAY_BILL;
            } else {
                // 校验失败(单号错误)
                STATUS = EnumScanCode.CODE_ILLEGAL;
            }
        }
        return STATUS;

    }

}
